package com.example.cardgameapp

import android.os.Bundle
import android.content.Intent
import android.widget.Button
import android.widget.EditText
import android.widget.RadioGroup
import android.widget.CheckBox
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.util.ArrayList
import androidx.activity.result.contract.ActivityResultContracts
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date

class EditInfoActivity : AppCompatActivity() {
    private var avatarPath: String? = null // 保存头像路径


    private val photoLauncher = registerForActivityResult(
        ActivityResultContracts.TakePicturePreview()
    ) { bitmap: Bitmap? ->
        bitmap?.let {
            // 保存头像到本地并记录路径
            avatarPath = saveAvatar(it)
            // 更新头像显示
            findViewById<ImageView>(R.id.iv_avatar).setImageBitmap(it)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_info)

        // 相机按钮点击事件：启动拍照
        val ibCamera = findViewById<ImageView>(R.id.ib_camera)
        ibCamera.setOnClickListener {
            photoLauncher.launch(null)
        }

        // 取消按钮：返回上一页
        val btnCancel = findViewById<Button>(R.id.btn_cancel)
        btnCancel.setOnClickListener {
            finish()
        }

        val btnSave = findViewById<Button>(R.id.btn_save)
        btnSave.setOnClickListener {
            val name = findViewById<EditText>(R.id.et_name).text.toString()
            val gender = when (findViewById<RadioGroup>(R.id.rg_gender).checkedRadioButtonId) {
                R.id.rb_male -> getString(R.string.radio_male)
                R.id.rb_female -> getString(R.string.radio_female)
                else -> "未选择"
            }
            val studentId = findViewById<EditText>(R.id.et_student_id).text.toString()
            val phone = findViewById<EditText>(R.id.et_phone).text.toString()
            val email = findViewById<EditText>(R.id.et_email).text.toString()

            val hobbies = ArrayList<String>()
            if (findViewById<CheckBox>(R.id.cb_travel).isChecked) hobbies.add(getString(R.string.check_travel))
            if (findViewById<CheckBox>(R.id.cb_read).isChecked) hobbies.add(getString(R.string.check_read))
            if (findViewById<CheckBox>(R.id.cb_game).isChecked) hobbies.add(getString(R.string.check_game))
            if (findViewById<CheckBox>(R.id.cb_shopping).isChecked) hobbies.add(getString(R.string.check_shopping))
            val hobbyStr = if (hobbies.isEmpty()) "无" else hobbies.joinToString("、")
            val resultIntent = Intent().apply {
                putExtra("name", name)
                putExtra("gender", gender)
                putExtra("studentId", studentId)
                putExtra("phone", phone)
                putExtra("email", email)
                putExtra("hobbies", hobbyStr)
                putExtra("avatarPath", avatarPath) // 回传头像路径
            }
            setResult(RESULT_OK, resultIntent)
            finish()
        }
    }

    private fun saveAvatar(bitmap: Bitmap): String {
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val dir = getExternalFilesDir(null)
        val file = File(dir, "avatar_$timeStamp.jpg")
        FileOutputStream(file).use {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it)
        }
        return file.absolutePath
    }
}